package com.gzxw.mogublog.xo.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzxw.mogublog.commons.entity.File;
import com.gzxw.mogublog.commons.entity.Picture;
import com.gzxw.mogublog.commons.feign.PictureFeignClient;
import com.gzxw.mogublog.utils.R;
import com.gzxw.mogublog.utils.StringUtils;
import com.gzxw.mogublog.xo.mapper.PictureMapper;
import com.gzxw.mogublog.xo.service.PictureService;
import com.gzxw.mogublog.xo.vo.PictureVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PictureServiceImpl extends ServiceImpl<PictureMapper, Picture> implements PictureService {

    @Autowired
    private PictureFeignClient pictureFeignClient;

    @Override
    public IPage<Picture> getPageList(PictureVO pictureVO) {
        QueryWrapper<Picture> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(pictureVO.getKeyword()) && !StringUtils.isEmpty(pictureVO.getKeyword().trim())) {
            queryWrapper.like("pic_name", pictureVO.getKeyword().trim());
        }
        Page<Picture> page = new Page<>();
        page.setCurrent(pictureVO.getCurrentPage());
        page.setSize(pictureVO.getPageSize());
        queryWrapper.eq("status", 1);
        queryWrapper.eq("picture_sort_uid", pictureVO.getPictureSortUid());
        queryWrapper.orderByDesc("create_time");
        IPage<Picture> pageList = this.page(page, queryWrapper);
        List<Picture> records = pageList.getRecords();

        List<String > fileUids = new ArrayList<>();
        records.forEach(item -> {
            fileUids.add(item.getFileUid());
        });

        R picResult = null;
        if (fileUids.size()>0) {
            picResult = pictureFeignClient.getPictures(fileUids);
        }

        assert picResult != null;
        List<File> pictureList = JSON.parseArray(picResult.getMessage(), File.class);
        Map<String ,String > pictureMap = new HashMap<>();
        pictureList.forEach(item -> {
            pictureMap.put(item.getUid(),item.getPicUrl());
        });

        records.forEach(item -> {
            if (StringUtils.isNotEmpty(item.getFileUid())) {
                item.setPictureUrl(pictureMap.get(item.getFileUid()));
            }
        });

        pageList.setRecords(records);
        return pageList;
    }

    @Override
    public String deleteBatchPicture(PictureVO pictureVO) {

        String uidStr = pictureVO.getUid();
        if (StringUtils.isEmpty(uidStr)) {
            return "参数输入有误！";
        }

        List<String> uids = StringUtils.changeStringToString(uidStr, ";");
        for (String item: uids) {
            Picture picture = this.getById(item);
            picture.setStatus(0);
            picture.setUpdateTime(new Date());
            picture.updateById();
        }
        return "删除成功";
    }
}
